﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace MultiThread
{
    public partial class Form1 : Form
    {
        private readonly Thread[] _threads = new Thread[5];
        private int _totalThread = 0;
        private readonly object _lockTotal = new object();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            _threads[0] = new Thread(Method1);
            _threads[1] = new Thread(Method2);
            _threads[2] = new Thread(Method3);
            _threads[3] = new Thread(Method4);
            _threads[4] = new Thread(Method5);
            for (int i = 0; i < 5; i++)
            {
                _threads[i].IsBackground = true;
                _threads[i].Start();
            }
        }

        private void Method1()
        {
            Thread.Sleep(1000);
            textBox1.Invoke((Action)(() => textBox1.Text = "Test"));
            label1.Invoke((Action)(() => label1.Text = MainResource.Method1));
            lock (_lockTotal)
            {
                _totalThread += 1;
                if (_totalThread == 5)
                {
                    Invoke((Action)(() => label6.Text = MainResource.MethodSuccess));
                }
            }
        }

        private void Method2()
        {
            Thread.Sleep(2000);
            label2.Invoke((Action)(() => label2.Text = MainResource.Method2));
            lock (_lockTotal)
            {
                _totalThread += 1;
                if (_totalThread == 5)
                {
                    Invoke((Action)(() => label6.Text = MainResource.MethodSuccess));
                }
            }
        }

        private void Method3()
        {
            Thread.Sleep(2000);
            label3.Invoke((Action)(() => label3.Text = MainResource.Method3));
            lock (_lockTotal)
            {
                _totalThread += 1;
                if (_totalThread == 5)
                {
                    Invoke((Action)(() => label6.Text = MainResource.MethodSuccess));
                }
            }
        }

        private void Method4()
        {
            Thread.Sleep(3000);
            label4.Invoke((Action)(() => label4.Text = MainResource.Method4));
            lock (_lockTotal)
            {
                _totalThread += 1;
                if (_totalThread == 5)
                {
                    Invoke((Action)(() => label6.Text = MainResource.MethodSuccess));
                }
            }
        }

        private void Method5()
        {

            Thread.Sleep(3000);
            label5.Invoke((Action)(() => label5.Text = MainResource.Method5));
            lock (_lockTotal)
            {
                _totalThread += 1;
                if (_totalThread == 5)
                {
                    Invoke((Action)(() => label6.Text = MainResource.MethodSuccess));
                }
            }
        }
    }
}
